﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ThreeDContentPipeline
{
    public class VertexBufferContent
    {
        public VertexBufferContent(VertexElement[] vertexElements)
        {
            VertexElement lastElement = vertexElements[vertexElements.Length - 1];

            SizeInBytes = lastElement.Offset;
            if (lastElement.VertexElementFormat == VertexElementFormat.Vector2)
            {
                SizeInBytes += 8;
            }
            else if (lastElement.VertexElementFormat == VertexElementFormat.Vector3)
            {
                SizeInBytes += 12;
            }
            else
            {
                SizeInBytes += 16; // must be a Vector4 then (other formats not supported)
            }

            VertexElements = vertexElements;

            Vertices = new List<float>();
        }

        public int SizeInBytes
        {
            get;
            private set;
        }

        public int VertexCount
        {
            get { return Vertices.Count * 4 / SizeInBytes; }
        }

        public VertexElement[] VertexElements
        {
            get;
            private set;
        }

        public List<float> Vertices
        {
            get;
            private set;
        }

        public void Add(Vector2 value)
        {
            Vertices.Add(value.X);
            Vertices.Add(value.Y);
        }

        public void Add(Vector3 value)
        {
            Vertices.Add(value.X);
            Vertices.Add(value.Y);
            Vertices.Add(value.Z);
        }

        public void Add(Vector4 value)
        {
            Vertices.Add(value.X);
            Vertices.Add(value.Y);
            Vertices.Add(value.Z);
            Vertices.Add(value.W);
        }
    }
}
